package com.hexiaohe.springboot_vue_app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexiaohe.springboot_vue_app.entity.User;
import com.hexiaohe.springboot_vue_app.entity.UserRole;
import com.hexiaohe.springboot_vue_app.service.UserRoleService;
import com.hexiaohe.springboot_vue_app.service.UserService;
import com.hexiaohe.springboot_vue_app.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserContronller {
    @Autowired
    UserService userService;
    @Autowired
    UserRoleService userRoleService;
    @PostMapping("/logn")
    public Result<User> login(@RequestBody User user, HttpSession session){
        Result<User>  result = userService.login(user);
        session.setAttribute("user_info",result.getData());
        System.out.println();
        return result;
    }


    @GetMapping("/getAllUser")
    public Result<?>getAllUser(@RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "10")Integer pageSize,
                               @RequestParam(defaultValue = "")String search){
        Page<User>userPage = userService.findPage(new Page<>(pageNum,pageSize),search);
        return Result.success(userPage);
    }
    @PostMapping("/addUser")
    public Result<?> save(@RequestBody User user){
        if (user.getPassword() ==null){
            user.setPassword("123");
        }
        try {
            userService.addUser(user);
            System.out.println(user.getRoles());
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(2);
            userRoleService.addUserRole(userRole);
            return Result.success();
        }
        catch (Exception e){
            return Result.error("-1","添加用户失败");
        }
    }

    @PutMapping("/editUser")
    public Result<?>update(@RequestBody User user){
        int row =userService.updateById(user);
        if (row>0){
            return Result.success();
        }else {
            return Result.error("-1","用户修改失败");
        }
    }
    @DeleteMapping("/deleteUserById/{id}")
    public Result<?> deleteUserById(@PathVariable int id){
        try {
            userRoleService.deleteUserById(id);
            int row = userService.deleteUserById(id);
            if (row>0){
                return Result.success();
            }else {
                return Result.error("-1","用户删除失败");
            }
        }
        catch (Exception e){
            return Result.error("-1","用户删除失败");
        }
    }
    @PostMapping("/register")
    public Result<?> registerUser(@RequestBody User user){
        boolean res = userService.getUserByUsername(user);
        if (res){
            int row =userService.registerUser(user);
            if (row>0){
                return Result.success();
            }else {
                return Result.error("-1","用户注册失败");
            }
        }else {
            return Result.error("-1","用户已存在");
        }
    }
    @PostMapping("/updateperson")
    public Result<?> updateUserPerson(@RequestBody User user){
        System.out.println(user);
        int row = userService.updatePerson(user);
        if (row>0){
            return Result.success(user);
        }else {
            return Result.error("-1","保存失败");
        }
    }
    @PostMapping("/updatePassword")
    public Result<?>updatePassword(@RequestParam("username")String username,
                                   @RequestParam("ypassword") String ypassword,
                                   @RequestParam("xpassword")String xpassword){
        boolean res =userService.selectUsername(username,ypassword);
        if (res){
            int row = userService.updatePassword(username,xpassword);
            if (row>0){
                return Result.success();
            }else {
                return Result.error("-1","修改失败");
            }
        }else {
            return Result.error("-1","原密码不正确");
        }

    }


}
